import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/page/Login'
import Home from '@/page/Home'
import Wellcome from '@/page/Wellcome'
import users from '@/page/user/users'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/', 
      redirect:'/Login' //路由重定向 Login组件
    },
    {
      path: '/Login',
      component: Login
    },
    {
      path: '/Home',
      component: Home,
      redirect:'/Wellcome', //路由重定向 Wellcome组件： 只要访问了home组件，就重定向到Wellcome路由
      children: [ 
        {path: '/Wellcome',component: Wellcome}, //home组件嵌套Wellcome路由
        {path: '/users',component: users}
      ] 
    }
  ]
})

//挂载路由导航守卫
router.beforeEach((to,from,next) => {
  /*
  **to 将要访问的路径
  **from 代表从哪个路径而来
  **next是一个函数，表示放行
  */
  if(to.path === '/Login') return next()
  //先获取token
  const tokenStr = window.sessionStorage.getItem('token')
  //判断token的值，为空时强制跳转登录页，否则放行
  if(!tokenStr) return next('/Login')
  next()

})

export default router
